package com.xyz.controller;

import com.xyz.aop.ObjectParam;
import com.xyz.aop.Param;
import com.xyz.config.SystemData;
import com.xyz.dto.RepairMaintenanceDto;
import com.xyz.dto.RepairMaintenanceInsDto;
import com.xyz.dto.RepairWorkOrderDto;
import com.xyz.dto.SelectPageAllDto;
import com.xyz.entity.vo.EquipmentInfoExportVo;
import com.xyz.entity.vo.RepairWorkOrderExportVo;
import com.xyz.service.CommonService;
import com.xyz.service.RepairAppService;
import com.xyz.service.RepairService;
import com.xyz.util.BeanUtils;
import com.xyz.util.ExcelUtils;
import com.xyz.util.dto.DataResult;
import com.xyz.util.dto.GridData;
import com.xyz.util.dto.LoginInfo;
import io.swagger.annotations.ApiModelProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("repair")
public class RepairController {

    private static final Logger logger = LoggerFactory.getLogger(RepairController.class);

    @Autowired
    private RepairService repairService;

    @Autowired
    private RepairAppService repairAppService;

    @Autowired
    private CommonService commonService;


    @PostMapping("/getRepairAll")
    public DataResult getRepairAll(LoginInfo loginInfo, @ObjectParam SelectPageAllDto selectPageAllDto) {
        try {
            return repairService.selectRepairAll(loginInfo, selectPageAllDto);
        } catch (Exception e) {
            logger.error("getRepairAll", e);
            return DataResult.build9500();
        }
    }

    @ApiModelProperty("导出维修列表")
    @PostMapping("/exportData")
    public DataResult exportData(LoginInfo loginInfo, @ObjectParam SelectPageAllDto selectPageAllDto, HttpServletResponse response) {
        try {
            DataResult dataResult = repairService.selectRepairAll(loginInfo, selectPageAllDto);
            Object obj = dataResult.getInfo();
            List<RepairWorkOrderExportVo> list = new ArrayList<>();
            if (obj != null && obj instanceof GridData) {
                GridData gridData = (GridData) obj;
                list = BeanUtils.toBean(gridData.getRows(), RepairWorkOrderExportVo.class);
                //设备状态
                Map<Integer, String> emStateMap = commonService.getDictMap("TYPE_EM_STATE");
                //故障现象
                Map<Integer, String> repFaultTypeMap = commonService.getDictMap("TYPE_REP_FAULT");
                //紧急程度
                Map<Integer, String> urgencyLevelMap = commonService.getDictMap("TYPE_URGENCY_LEVEL");
                for (RepairWorkOrderExportVo vo : list) {
                    vo.setRepEmStateName(emStateMap.get(vo.getRepEmState()));
                    vo.setRepFaultTypeName(repFaultTypeMap.get(vo.getRepFaultType()));
                    vo.setUrgencyLevelName(urgencyLevelMap.get(vo.getUrgencyLevel()));
                }
            }
            ExcelUtils.write(response, "维修管理.xls", "维修管理", RepairWorkOrderExportVo.class,
                    list);
            return DataResult.build9100();
        } catch (Exception e) {
            logger.error("exportData", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("/getRepairAllByEqId")
    public DataResult getRepairAllByEqId(LoginInfo loginInfo, @ObjectParam SelectPageAllDto selectPageAllDto) {
        try {
            return repairService.getRepairAllByEqId(loginInfo, selectPageAllDto);
        } catch (Exception e) {
            logger.error("getRepairAllByEqId", e);
            return DataResult.build9500();
        }
    }

    /*
     * 根据ID查询报修单详情
     * */
    @PostMapping("/getRepairById")
    public DataResult getRepairById(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String repId
    ) {
        try {
            return repairService.getRepairById(loginInfo, repId);
        } catch (Exception e) {
            logger.error("getRepairById", e);
            return DataResult.build9500();
        }
    }

    /*
     * 根据ID查询报修单基本信息
     * */
    @PostMapping("/getBasicRepairById")
    public DataResult getBasicRepairById(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String repId
    ) {
        try {
            return repairService.getBasicRepairById(loginInfo, repId);
        } catch (Exception e) {
            logger.error("getBasicRepairById", e);
            return DataResult.build9500();
        }
    }

    /*
     * 根据ID修改报修单基本信息
     * */
    @PostMapping("/getBasicRepairUpdateById")
    public DataResult getBasicRepairUpdateById(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String repId,
            @Param(isRequired = false) String maintenEmployeeUsersId
    ) {
        try {
            return repairService.getBasicRepairUpdateById(loginInfo, repId,maintenEmployeeUsersId);
        } catch (Exception e) {
            logger.error("getBasicRepairById", e);
            return DataResult.build9500();
        }
    }

    /*
     * 根据ID查询报修单审核信息
     * */
    @PostMapping("/getAnnounceRepairById")
    public DataResult getAnnounceRepairById(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String repId,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String repState
    ) {
        try {
            return repairService.getAnnounceRepairById(loginInfo, repId, repState);
        } catch (Exception e) {
            logger.error("getAnnounceRepairById", e);
            return DataResult.build9500();
        }
    }

    /*
     * 根据ID查询报修单维修信息
     * */
    @PostMapping("/getMaintenanceRepairById")
    public DataResult getMaintenanceRepairById(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String repId,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String repState
    ) {
        try {
            return repairService.getMaintenanceRepairById(loginInfo, repId, repState);
        } catch (Exception e) {
            logger.error("getMaintenanceRepairById", e);
            return DataResult.build9500();
        }
    }

    /*
     * 根据维修ID查询验收信息
     * */
    @PostMapping("/getAcceptById")
    public DataResult getAcceptById(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String repId,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String repState
    ) {
        try {
            return repairService.getAcceptById(loginInfo, repId, repState);
        } catch (Exception e) {
            logger.error("getAcceptById", e);
            return DataResult.build9500();
        }
    }

    /*
     * 工单流水
     * */
    @PostMapping("/getWorkOrderFlow")
    public DataResult getWorkOrderFlow(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String repId
    ) {
        try {
            return repairService.getWorkOrderFlow(loginInfo, repId);
        } catch (Exception e) {
            logger.error("getWorkOrderFlow", e);
            return DataResult.build9500();
        }
    }


    /*
     * 报修
     * */
    @PostMapping("/insertRepair")
    public DataResult insertRepair(LoginInfo loginInfo, @ObjectParam RepairWorkOrderDto repairDto) {
        try {
            synchronized (SystemData.repairLock) {
                return repairService.insertRepair(loginInfo, repairDto);
            }
        } catch (Exception e) {
            logger.error("insertRepair", e);
            return DataResult.build9500();
        }
    }


    /*
     * 领取工单
     * receiveType = 1 自己领取
     * receiveType = 2 领导派单
     * */
    @PostMapping("/receiveRepair")
    public DataResult receiveRepair(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String repId,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String receiveType,
            @Param(type = Param.ParamType.POSITIVE_INTEGER, isRequired = false) String employeeId
    ) {
        try {
            return repairService.receiveRepair(loginInfo, repId, receiveType, employeeId);
        } catch (Exception e) {
            logger.error("receiveRepair", e);
            return DataResult.build9500();
        }
    }

    /*
     * 手动点击维修中
     * */
    @PostMapping("/undeRepair")
    public DataResult undeRepair(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return repairService.undeRepair(loginInfo, id);
        } catch (Exception e) {
            logger.error("undeRepair", e);
            return DataResult.build9500();
        }
    }

    /*
     * 拒单
     * */
    @PostMapping("/refuseRepair")
    public DataResult refuseRepair(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return repairService.refuseRepair(loginInfo, id);
        } catch (Exception e) {
            logger.error("refuseRepair", e);
            return DataResult.build9500();
        }
    }

    /*
     * 暂停和开始维修
     * type;1,暂停；2开始维修
     * */
    @PostMapping("/suspendRepair")
    public DataResult suspendRepair(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String type
    ) {
        try {
            return repairService.suspendRepair(loginInfo, id, type);
        } catch (Exception e) {
            logger.error("suspendRepair", e);
            return DataResult.build9500();
        }
    }

    /*
     * 开始维修保存
     * */
    @PostMapping("/insertSuspendRepair")
    public DataResult insertSuspendRepair(
            LoginInfo loginInfo,
            @ObjectParam RepairMaintenanceInsDto repairMaintenanceInsDto
    ) {
        try {
            return repairService.insertSuspendRepair(loginInfo, repairMaintenanceInsDto);
        } catch (Exception e) {
            logger.error("insertSuspendRepair", e);
            return DataResult.build9500();
        }
    }

    /*
     * 工单转交
     * */
    @PostMapping("/transferRepair")
    public DataResult transferRepair(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return repairService.transferRepair(loginInfo, id);
        } catch (Exception e) {
            logger.error("transferRepair", e);
            return DataResult.build9500();
        }
    }

    /*
     * 点击维修完成
     * */
    @PostMapping("/completeRepair")
    public DataResult completeRepair(
            LoginInfo loginInfo,
            @ObjectParam RepairMaintenanceDto repairMaintenanceDto
    ) {
        try {
            return repairService.completeRepair(loginInfo, repairMaintenanceDto);
        } catch (Exception e) {
            logger.error("completeRepair", e);
            return DataResult.build9500();
        }
    }

    /*
     * 验收指派人员
     * */
    @PostMapping("/accepAssignRepair")
    public DataResult accepAssignRepair(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String employeeId
    ) {
        try {
            return repairService.accepAssignRepair(loginInfo, id, employeeId);
        } catch (Exception e) {
            logger.error("accepAssignRepair", e);
            return DataResult.build9500();
        }
    }


    /**
     * 验收
     **/
    @PostMapping("/acceptRepair")
    public DataResult acceptRepair(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String type,
            @Param(isRequired = false) String acceptExplain,
            @Param(isRequired = false) String acceptScore
    ) {
        try {
            return repairService.acceptRepair(loginInfo, id, type, acceptExplain, acceptScore);
        } catch (Exception e) {
            logger.error("acceptRepair", e);
            return DataResult.build9500();
        }
    }

    /*
     * ----APP----
     * */

    /*
     * App待审核查询
     * */
    @PostMapping("/getPendingRepairAll")
    public DataResult getPendingRepairAll(LoginInfo loginInfo, @ObjectParam SelectPageAllDto selectPageAllDto) {
        try {
            return repairAppService.getPendingRepairAll(loginInfo, selectPageAllDto);
        } catch (Exception e) {
            logger.error("getPendingRepairAll", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("/getRepairWait")
    public DataResult getRepairWait(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String order
    ) {
        try {
            return repairService.getRepairWait(li, Integer.parseInt(order));
        } catch (Exception e) {
            logger.error("getRepairWait", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("/getRepairEmployee")
    public DataResult getRepairEmployee(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageNum,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageSize,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String day
    ) {
        try {
            return repairService.getRepairEmployee(li, Integer.parseInt(pageNum), Integer.parseInt(pageSize), Integer.parseInt(day));
        } catch (Exception e) {
            logger.error("getRepairEmployee", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("/updateRepairWait")
    public DataResult updateRepairWait(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String order,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String state,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String employeeId,
            @Param String startTime
    ) {
        try {
            return repairService.updateRepairWait(li, Integer.parseInt(order), Long.parseLong(id), Integer.parseInt(state), Long.parseLong(employeeId), startTime);
        } catch (Exception e) {
            logger.error("updateRepairWait", e);
            return DataResult.build9500();
        }
    }

}
